package player_event

import (
	"errors"
	"fmt"
	"gate_server/model"
	"proto/base"
	"proto/server/gate"
)

type (
	onLoginEvent struct {
	}
	onLoginCallback = func(msg *proto_server_gate.OnLogin, player *model.Player)
)

var OnLogin = new(onLoginEvent)

func (event *onLoginEvent) Verify(callback interface{}) (err error) {
	if _, ok := callback.(onLoginCallback); !ok {
		err = errors.New("not on login event callback function")
	}

	return
}

func (event *onLoginEvent) Call(callback interface{}, args []interface{}) (err error) {
	const ArgsCount = 1
	if len(args) < ArgsCount {
		err = fmt.Errorf("on login event callback need %d arg", ArgsCount)
		return
	}

	call := callback.(onLoginCallback)
	i := 0
	msg := args[i].(*proto_server_gate.OnLogin)
	i++
	player := args[i].(*model.Player)
	i++

	call(msg, player)

	return
}

type (
	onLogoutEvent struct {
	}
	onLogoutCallback = func(player *model.Player, reason proto_base.LogoutReason)
)

var OnLogout = new(onLogoutEvent)

func (event *onLogoutEvent) Verify(callback interface{}) (err error) {
	if _, ok := callback.(onLogoutCallback); !ok {
		err = errors.New("not player on logout event callback function")
	}

	return
}

func (event *onLogoutEvent) Call(callback interface{}, args []interface{}) (err error) {
	const ArgsCount = 2
	if len(args) < ArgsCount {
		err = fmt.Errorf("player on logout event callback need %d arg", ArgsCount)
		return
	}

	call := callback.(onLogoutCallback)
	i := 0
	player := args[i].(*model.Player)
	i++
	reason := args[i].(proto_base.LogoutReason)
	i++

	call(player, reason)

	return
}
